<template lang="">
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog v-dialogDrag :title="`${showText}权限点`" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
  import { updatePermissionAPI, getPermissionDetailAPI, addPermissionAPI, } from '@/api'
  export default {
    props: {
      showDialog: {
        type: Boolean,
        default: false
      },
      showText: {
        type: String
      },
      userId: {
        require: true
      },
      pid: {
        default: ""
      },
      type1: {
        default: 1
      }
    },
    data () {
      return {
        formData: {
          name: '', // 名称
          code: '', // 标识
          description: '', // 描述
          type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
          pid: '', // 因为做的是树 需要知道添加到哪个节点下了
          enVisible: '0' // 开启
        },
        rules: {
          name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' },
          { min: 2, max: 6, message: '权限名称必须在2-6位之间', trigger: 'blur' },
          ],
          code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      btnCancel () {
        this.$emit('update:showDialog', false)
        this.$refs.perForm.resetFields()
      },
      async getPermissionDetail (id) {
        this.formData = await getPermissionDetailAPI(id)
      },
      handleDragClick (e) {
        // 判断拖拽组件的状态
        let isDrag = false;
        try {
          isDrag = e.target.getAttribute('ele-flag') === 'true';
        } catch (e) {
        }
        if (isDrag) {
          return;
        }
        // 当推拽组件未在 拖动状态 执行点击事件
        // todo 下面是执行点击事件的代码
      },
      btnOK () {
        // 进行表单校验
        this.$refs.perForm.validate(async (is_OK) => {
          if (is_OK) {
            if (this.showText === '编辑') {
              const a = await updatePermissionAPI({ id: this.userId, ...this.formData })
              this.$message.success('修改成功')
            } else {
              this.formData.pid = this.pid
              this.formData.type = this.type1
              await addPermissionAPI(this.formData)
              this.$message.success('新增成功')
            }
            this.$emit('update')
            this.btnCancel()
          }
        })
      }
    },
  }
</script>
<style lang="">

</style>